package com.zhkj.txg.module.login.ui

import android.content.Intent
import android.text.TextUtils
import android.util.Log
import android.view.View
import androidx.lifecycle.Observer
import com.zhkj.lib.base.ViewModelActivity
import com.zhkj.lib.http.HttpResponseStatus
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.R
import com.zhkj.txg.base.GlideApp
import com.zhkj.txg.module.home.ui.MainActivity
import com.zhkj.txg.module.login.vm.SplashViewModel
import com.zhkj.txg.utils.ConfigManager
import com.zhkj.txg.utils.ImageUtils
import com.zhkj.txg.utils.typeJump
import kotlinx.android.synthetic.main.activity_splash.*

class SplashActivity : ViewModelActivity<SplashViewModel>() {
    override val layoutResId: Int = R.layout.activity_splash
    override fun setViewModel(): SplashViewModel = createViewModel()

    override fun initData() {
        if (ConfigManager.getIsFirstRun()) {
            // 首次进入，进入引导页
            ConfigManager.saveFirstRun(false)
            startActivity(Intent(this, GuideActivity::class.java))
            finish()
        } else if (!TextUtils.isEmpty(AccountManager.getToken())) {
            Thread {
                Thread.sleep(500)
                viewModel.splash()
            }.start()
        } else jump2Login()
    }

    override fun initViewModel() {
        viewModel.splashAdLiveData.observe(this, Observer { response ->
            handlerResponseStatus(response.status, response.statusTip)
            if (response.status == HttpResponseStatus.Status.SUCCESS) {
                if (response.response?.data == null) jump2Login()
                else {
                    GlideApp.with(this).load(response.response?.data?.image ?: "").error(R.drawable.ic_loading).into(ivSkip)
                    Thread {
                        Thread.sleep(500)
                        runOnUiThread {
                            viewModel.startCountDown()
                            // 拉取广告数据,显示广告
                            ivBackgroundImage.visibility = View.GONE
                            ivSkip.visibility = View.VISIBLE
                            tvSkip.visibility = View.VISIBLE
                        }
                    }.start()
                }
            } else if (response.status == HttpResponseStatus.Status.FAILURE) jump2Login()
        })
        viewModel.countDownLiveData.observe(this, Observer {
            if (it >= 0L) tvSkip.text = "跳过(${it}秒)"
            if (it == 0L) jump2Login()
        })
    }

    override fun initUI() {
        tvSkip.setOnClickListener {
            viewModel.onCleared()
            jump2Login()
        }
        ivSkip.setOnClickListener {
            viewModel.splashAdLiveData.value?.response?.data?.let {
                if (it.jumpType != null && it.jumpType.toInt() > 0) {
                    viewModel.onCleared()
                    startActivity(Intent(this, MainActivity::class.java))
                    typeJump(this, it.jumpType.toInt(), it.params, it.title)
                    finish()
                }
            }
        }
    }

    private fun jump2Login() {
        if (!TextUtils.isEmpty(AccountManager.getToken())) {
            startActivity(
                Intent(this, MainActivity::class.java)
                    .setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP or Intent.FLAG_ACTIVITY_CLEAR_TOP)
            )
        } else LoginActivity.open2Login(this)
        finish()
    }
}